package resource

import (
	"awesomeTask/system/helper"
	"awesomeTask/system/kernel"
	"errors"
)

type ResourceManager interface {
	FindString(fileName string) (string, error)
	FindAbsPath(fileName string) (string, error)
}

type ResourceManagerObj struct {
	ResourceDir string
}

func (manager ResourceManagerObj) FindString(path string) (string, error) {
	absPath := manager.ResourceDir + path
	if helper.CheckFileIsExist(absPath) {
		return helper.ReadFileAsString(absPath), nil
	}
	return "", errors.New("文件不存在" + absPath)
}
func (manager ResourceManagerObj) FindAbsPath(path string) (string, error) {
	absPath := manager.ResourceDir + path
	if helper.CheckFileIsExist(absPath) {
		return absPath, nil
	}
	return "", errors.New("文件不存在" + absPath)
}
func GetResourceManager() ResourceManager {
	instance := kernel.GetIocInstance()
	return instance.GetInstanceOrCreate("resource").(ResourceManager)
}
